<?php

namespace App\Services\User;

use App\Exceptions\ServiceException;
use App\Models\User\Role;
use App\Services\Auth\MenuService;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use \Exception;

class RoleService
{
    private $title = '角色管理';
    /**
     * @Notes:添加角色
     * @author: Liao Hong
     * @Time: 2025/10/20   13:30
     * @Interface add
     */
    public function add($params = []){
       $params['create_by'] = user()->user_name;
        Db::beginTransaction();
        try {
            //菜单id
            $menuIds = [];
            if(isset($params['menuIds']) && !empty($params['menuIds'])){
                $menuIds = $params['menuIds'];
                unset($params['menuIds']);
            }
            $roleId = Role::query()->insertGetId($params);
            if(!empty($menuIds)){
                $menuRole = [];
                foreach ($menuIds as $item){
                    $menuRole[] = [
                        'menu_id' =>$item,
                        'role_id' =>$roleId,
                    ];
                }
                DB::table('role_menu')->insert($menuRole);
            }
            DB::commit();
            addLog($this->title,'1',true);
            return true;
        }catch (Exception $exception){
            addLog($this->title,'1',false);
            DB::rollBack();
            throw new ServiceException($exception->getMessage(), 201);
        }
    }

    /**
     * @Notes:获取单条角色信息
     * @param array $params
     * @author: Liao Hong
     * @Time: 2025/10/21   14:33
     * @Interface getRow
     */
    public function getRow($params=[])
    {
        $row = Role::query()->where('role_id',$params['role_id'])->first();
        return $row->toArray();
    }
    /**
     * @Notes:修改角色
     * @author: Liao Hong
     * @Time: 2025/10/20   13:30
     * @Interface add
     */
    public function update($params = []){
        $params['update_by'] = user()->user_name;
        //菜单id
        Db::beginTransaction();
        try {
            $menuIds = [];
            if(isset($params['menuIds']) && !empty($params['menuIds'])){
                $menuIds = $params['menuIds'];
                unset($params['menuIds']);
            }
            if(!empty($menuIds)){
                $menuRole = [];
                foreach ($menuIds as $item){
                    $menuRole[] = [
                        'menu_id' => $item,
                        'role_id' =>$params['role_id'],
                    ];
                }
                //删除旧关联
                DB::table('role_menu')->where('role_id',$params['role_id'])->delete();
                //添加新关联
                DB::table('role_menu')->insert($menuRole);
            }
            Role::query()->where('role_id',$params['role_id'])->update($params);
            addLog($this->title,'2',true);
            DB::commit();
            return true;
        }catch (\Exception $exception){
            addLog($this->title,'2',false);
            DB::rollBack();
            throw new ServiceException($exception->getMessage(), 201);
        }
    }
    /**
     * @Notes:删除角色
     * @author: Liao Hong
     * @Time: 2025/10/20   13:30
     * @Interface add
     */
    public function delete($params = []){
        //删除菜单角色关联
        DB::table('role_menu')->where('role_id',$params['role_id'])->delete();
        //删除角色
        Role::query()->where('role_id',$params['role_id'])->delete();
        addLog($this->title,'3',true);
        return true;
    }
    /**
     * @Notes:角色列表
     * @author: Liao Hong
     * @Time: 2025/10/20   13:30
     * @Interface add
     */
    public function list($parmas){
        $query = [];
        if(isset($parmas['role_name']) && !empty($parmas['role_name'])){
            $query[] = ['role_name','like','%'.$parmas['role_name'].'%'];
        }
        if(isset($parmas['role_key']) && !empty($parmas['role_key'])){
            $query[] = ['role_key','like','%'.$parmas['role_key'].'%'];
        }
        if(isset($parmas['status']) && !empty($parmas['status'])){
            $query[] = ['status','=',$parmas['status']];
        }
        $limit = isset($parmas['limit']) ? $parmas['limit'] : 10;

        $row = Role::query()->where($query)->paginate($limit);
        $row = empty($row) ?  [] :$row->toArray();
        return $row;
    }

    /**
     * @Notes:获取角色所选的菜单
     * @param $params
     * @author: Liao Hong
     * @Time: 2025/10/22   15:13
     * @Interface roleMenuTreeselect
     */
    public function roleMenuTreeselect($params)
    {
        $row =  DB::table('role_menu')->where('role_id',$params['role_id'])->pluck('menu_id')->toArray();
        /** @var MenuService $menuService */
        $menuApp = app(MenuService::class);
        $menu = $menuApp->treeselect();
        $result = [
            'checkedKeys' => $row,
            'menus'       => $menu,
        ];
        return $result;
    }
}
